package cn.kgc.javaweb.servlet;

import cn.hutool.json.JSONUtil;
import cn.kgc.javaweb.entity.Student;
import cn.kgc.javaweb.service.StudentService;
import cn.kgc.javaweb.service.impl.StudentServiceImpl;
import cn.kgc.javaweb.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/3/24
 */
@WebServlet("/student")
public class StudentServlet extends BaseServlet {

    private StudentService studentService = new StudentServiceImpl();
    //添加学生信息
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("添加学生请求");
        String stuName = req.getParameter("stuName");
        String age = req.getParameter("age");
        String tel = req.getParameter("tel");
        String sex = req.getParameter("sex");
        String address = req.getParameter("address");

        Student student = Student.builder().stuName(stuName).age(Integer.parseInt(age)).sex(sex).address(address).tel(tel).build();
        boolean b =   studentService.addStudent(student);
        //判定是否添加成功
        HashMap<String, Object> res = new HashMap<>();
        if(b){
            res.put("code", 200);
            res.put("msg", "添加成功");

        }else{
            res.put("code", 500);
            res.put("msg", "添加失败");
        }
        // 以json格式将后台处理的结果返回   java对象  res  ->json
        String s = JSONUtil.toJsonStr(res);
        //设置响应格式  json
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(s);
        writer.close();
    }

    //删除学生信息
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id = req.getParameter("id");
        boolean  b=   studentService.delStudent(Integer.parseInt(id));
        //判定是否删除成功
        HashMap<String, Object> res = new HashMap<>();
        if(b){
            res.put("code", 200);
            res.put("msg", "删除成功");

        }else{
            res.put("code", 500);
            res.put("msg", "删除失败");
        }
        // 以json格式将后台处理的结果返回   java对象  res  ->json
        String s = JSONUtil.toJsonStr(res);
        //设置响应格式  json
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(s);
        writer.close();

    }

    //修改学生信息
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("更新学生信息");
        String id = req.getParameter("id");
        String stuName = req.getParameter("stuName");
        String age = req.getParameter("age");
        String tel = req.getParameter("tel");
        String sex = req.getParameter("sex");
        String address = req.getParameter("address");

        Student student = Student.builder().id(Integer.parseInt(id)).stuName(stuName).age(Integer.parseInt(age)).sex(sex).address(address).tel(tel).build();

        System.out.println("student = " + student);
        boolean b =   studentService.updateStudent(student);
        //判定是否添加成功
        HashMap<String, Object> res = new HashMap<>();
        if(b){
            res.put("code", 200);
            res.put("msg", "更新成功");

        }else{
            res.put("code", 500);
            res.put("msg", "更新失败");
        }
        // 以json格式将后台处理的结果返回   java对象  res  ->json
        String s = JSONUtil.toJsonStr(res);
        //设置响应格式  json
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write(s);
        writer.close();
    }

    //查询学生信息
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取分页参数
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        Result<List<Student>> result = studentService.selectByPage(Integer.parseInt(page), Integer.parseInt(limit));
        // 将result 转换为json格式数据 直接返回
        resp.setContentType("application/json;charset=utf-8");
        String s = JSONUtil.toJsonStr(result);
        PrintWriter writer = resp.getWriter();
        writer.write(s);
        writer.close();
    }
}
